package com.liujunwen.demo.controller;

import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageBuilder;
import org.springframework.amqp.core.MessageDeliveryMode;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.support.converter.AbstractJavaTypeMapper;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("producer")
public class ProducerController {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Autowired
    private Environment env;

    @Value("${mq.queue.name}")
    public String queue;

    @Value("${mq.routing.key.name}")
    public String routingKey;

    @Value("${mq.exchange.name}")
    public String exchange;

    /**
     * 消息发送到默认队列
     *
     * @param info
     * @return
     */
    @RequestMapping(value = "/send", method = RequestMethod.GET)
    public String send(@RequestParam("info") String info) {
        try {
            rabbitTemplate.setMessageConverter(new Jackson2JsonMessageConverter());
            rabbitTemplate.setQueue(queue);
            rabbitTemplate.setExchange(exchange);
            rabbitTemplate.setRoutingKey(routingKey);
            byte[] bytes = info.getBytes("utf-8");
            // 持久化
            Message message = MessageBuilder.withBody(bytes).setDeliveryMode(MessageDeliveryMode.PERSISTENT).build();
            // 消息内容头：__ContentTypeId__ = "application/json"
            message.getMessageProperties().setHeader(AbstractJavaTypeMapper.DEFAULT_CONTENT_CLASSID_FIELD_NAME, MessageProperties.CONTENT_TYPE_JSON);
//            message.getMessageProperties().setPriority(20); //优先级
            rabbitTemplate.convertAndSend(message);
        } catch (Exception e) {
            e.printStackTrace();
            return e.toString();
        }
        return "Success";
    }

    /**
     * 消息发送到指定队列
     *
     * @param info
     * @return
     */
    @RequestMapping(value = "/send2queue", method = RequestMethod.GET)
    public String send(@RequestParam("info") String info, @RequestParam("queue") String queue, @RequestParam("exchange") String exchange, @RequestParam("routingKey") String routingKey) {
        try {
            rabbitTemplate.setMessageConverter(new Jackson2JsonMessageConverter());
            rabbitTemplate.setQueue(queue);
            rabbitTemplate.setExchange(exchange);
            rabbitTemplate.setRoutingKey(routingKey);
            byte[] bytes = info.getBytes("utf-8");
            // 持久化
            Message message = MessageBuilder.withBody(bytes).setDeliveryMode(MessageDeliveryMode.PERSISTENT).build();
            // 消息内容头：__ContentTypeId__ = "application/json"
            message.getMessageProperties().setHeader(AbstractJavaTypeMapper.DEFAULT_CONTENT_CLASSID_FIELD_NAME, MessageProperties.CONTENT_TYPE_JSON);
//            message.getMessageProperties().setPriority(20); //优先级
            rabbitTemplate.convertAndSend(message);
        } catch (Exception e) {
            e.printStackTrace();
            return e.toString();
        }
        return "Success";
    }
}
